#include "dwgrectitem.h"
#include <QBrush>
#include <QPainter>
#include <math.h>
#include <QtMath>
#include <QGraphicsSceneMouseEvent>

DwgRectItem::DwgRectItem() {}

DwgRectItem::DwgRectItem(QPointF pt1, QPointF pt2)
{
    mLTPt = pt1;
    mRBPt = pt2;
}

void DwgRectItem::DrawSnapRect(QPainter *painter, QPointF cpt)
{
    painter->save();
    QBrush brush(Qt::blue);
    painter->setBrush(brush);
    painter->drawRect(cpt.x()-5,cpt.y()-5,10,10);
    painter->restore();
}


QRectF DwgRectItem::boundingRect() const
{

    qreal w = fabs(mRBPt.x()-mLTPt.x());
    qreal h = fabs(mRBPt.y()-mLTPt.y());
    QPointF cenPt((mRBPt.x()+mLTPt.x())/2,(mRBPt.y()+mLTPt.y())/2);
    return QRectF(cenPt.x()-w/2,cenPt.y()-h/2,w,h);//.adjusted(-5,-5,5,5);
}
// shape未使用？
QPainterPath DwgRectItem::shape() const
{
    QPainterPath path;
    qreal w = fabs(mRBPt.x()-mLTPt.x());
    qreal h = fabs(mRBPt.y()-mLTPt.y());
    QPointF cenPt((mRBPt.x()+mLTPt.x())/2,(mRBPt.y()+mLTPt.y())/2);
    path.addRect(QRectF(cenPt.x()-w/2,cenPt.y()-h/2,w,h).adjusted(-5,-5,5,5));
    path.addRect(QRectF(cenPt.x()-w/2,cenPt.y()-h/2,w,h).adjusted(5,5,-5,-5));
    path.addRect(QRectF(cenPt.x()-5,cenPt.y()-5,10,10));
    return path;
}

void DwgRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qreal w = fabs(mRBPt.x()-mLTPt.x());
    qreal h = fabs(mRBPt.y()-mLTPt.y());
    QPointF cenPt((mRBPt.x()+mLTPt.x())/2,(mRBPt.y()+mLTPt.y())/2);
    QPen pen(mColor, mLineWidth, mPenStyle);
    painter->setPen(pen);
    painter->drawRect(cenPt.x()-w/2,cenPt.y()-h/2,w,h);
    if(this->isSelected())
    {
        DrawSnapRect(painter,cenPt);
        QPointF pt1;
        pt1 = cenPt + QPointF(w/2,-h/2);

        DrawSnapRect(painter,pt1);

    }
}

int Changept1 = 0;
void DwgRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        Changept1 = 0;
        QPointF curpt =  event->scenePos();

        QPointF cenPt((mRBPt.x()+mLTPt.x())/2,(mRBPt.y()+mLTPt.y())/2);

        // 为了捕捉到矩形的2个特征点，我们需要比较拾取点与这2个点之间的距离，这里直接输入5为距离
        qreal dist;
        dist = sqrt(pow((cenPt.x()-curpt.x()),2)+pow((cenPt.y()-curpt.y()),2));
        if(dist < 8)
            Changept1 = 1;   // 捕捉到矩形的中心点
        dist = sqrt(pow((mRBPt.x()-curpt.x()),2)+pow((mRBPt.y()-curpt.y()),2));
        if(dist < 8)
            Changept1 = 2;   // 捕捉到矩形的右下角点
    }
    QGraphicsItem::mousePressEvent(event);
}

void DwgRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qreal w = fabs(mRBPt.x()-mLTPt.x());
    qreal h = fabs(mRBPt.y()-mLTPt.y());
    QPointF cenPt((mRBPt.x()+mLTPt.x())/2,(mRBPt.y()+mLTPt.y())/2);

    if(Changept1 == 1)
    {
        QPointF size = event->scenePos()-cenPt; // 计算一下平移变换
        mLTPt += size;       //  把左上角和右下角整体平移
        mRBPt += size;
    }
    if(Changept1 == 2)      // 左下角点，用来拉大或拉小矩形
    {

        mRBPt = event->scenePos();       //  这里先直接把鼠标当前点给到矩形左下角
        // 如果要实现中心点不变，应该如何实现？

    }
    this->update();
}
